/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 * <p>
 * https://www.renren.io
 * <p>
 * 版权所有，侵权必究！
 */

package com.cn.xiaonuo.tcrfinance.hreports.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cn.xiaonuo.common.entity.BusinessMessage;
import com.cn.xiaonuo.tcrfinance.hreports.entity.PvGeneralStatement;
import com.cn.xiaonuo.tcrfinance.hreports.mapper.GenStaMapper;
import com.cn.xiaonuo.tcrfinance.hreports.mapper.PvGeneralStatementMapper;
import com.cn.xiaonuo.tcrfinance.hreports.service.IGenStaService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class GenStaServiceImpl extends ServiceImpl<GenStaMapper,String> implements  IGenStaService {

    @Autowired
    private GenStaMapper genStaMapper;
    @Autowired
    private PvGeneralStatementMapper pvGeneralStatementMapper;

    @Override
    public BusinessMessage selectGenStaLists(Map<String, Object> params) {
        BusinessMessage<Object> message = new BusinessMessage<>();
        Integer pageSize = Integer.valueOf(params.get("pageSize") + "");
        Integer pageNum = Integer.valueOf(params.get("pageNo") + "");
        if (null == pageNum || Integer.valueOf(pageNum) <= 1) {
            pageNum = 1;
        }
        if (null == pageSize || Integer.valueOf(pageSize) <= 1) {
            pageSize = 10;
        }
        PageHelper.startPage(pageNum, pageSize);
        Long beginTime =0L;
        Long endTime = 999999999999L;
        if (null != params.get("beginTime") && !"".equals(params.get("beginTime"))&& null != params.get("endTime")&& !"".equals(params.get("endTime"))) {
            beginTime = Long.valueOf(params.get("beginTime") + "");
            endTime = Long.valueOf(params.get("endTime") + "");
        }
        SimpleDateFormat ymdhms = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String begin = null;
        String end = null;
        if (null != beginTime && null != endTime) {
            begin = ymdhms.format(new Date(beginTime));
            end = ymdhms.format(new Date(endTime));
        }
        List<Map<String, Object>> resultList = genStaMapper.selectGenStaList(begin, end, null);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(resultList);
        if (null != resultList && resultList.size() >= 0) {
            message.setCode("200");
            message.setData(pageInfo);
            message.setSuccess(true);
            message.setMessage("查询成功");
            return message;
        }
        message.setMessage("查询失败");
        message.setCode("-1");
        message.setData(pageInfo);
        message.setSuccess(false);
        return message;
    }

    @Override
    public BusinessMessage tjGenSta() {
        BusinessMessage<Object> message = new BusinessMessage<>();
        String curTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        List<Map<String, Object>> resultList = genStaMapper.selectGenStaList(null, null, curTime);
        if (resultList.size() > 0) {
            message.setData(resultList);
            message.setCode("-1");
            message.setSuccess(false);
            message.setMessage("当天已经统计");
            return message;
        }
        Integer curDayRegNum = genStaMapper.curDayRegNum();
        if (null == curDayRegNum) {
            curDayRegNum = 0;
        }
        Double rjMoney = genStaMapper.rjMoney();
        if (null == rjMoney) {
            rjMoney = 0d;
        }
        Double cjMoney = genStaMapper.cjMoney();
        if (null == cjMoney) {
            cjMoney = 0d;
        }
        PvGeneralStatement pvGeneralStatement = new PvGeneralStatement();
        pvGeneralStatement.setCurNum(curDayRegNum);
        pvGeneralStatement.setCzMoney(BigDecimal.valueOf(rjMoney));
        pvGeneralStatement.setCjMoney(BigDecimal.valueOf(cjMoney));
        pvGeneralStatement.setCreatedAt(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        pvGeneralStatement.setUpdatedAt(new Date());
        int i = pvGeneralStatementMapper.insert(pvGeneralStatement);
        if (i == 1) {
            message.setMessage("统计成功");
            message.setSuccess(true);
            message.setCode("200");
            message.setData(i);
            return message;
        }
        message.setData(i);
        message.setCode("-1");
        message.setSuccess(false);
        message.setMessage("统计失败");
        return message;
    }
}
